package com.minew.beaconset.utils;

import android.text.TextUtils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * Create by 游德禄 on 2019-9-9
 * 日期处理工具
 */
public class DateUtil {
    /**
     * 把日期转换转指定格式的字符串
     * @param date
     * @param fmt
     * @return
     */
    public static String format(Date date, String fmt){
        return format(date,fmt,null);
    }
    public static String format(Date date, String fmt, String tip){
        if(date==null){
            if(tip!=null){
                return tip;
            }
            return "";
        }
       if(fmt==null){
           fmt = "yyyy-MM-dd HH:mm:ss";
       }
        SimpleDateFormat dateFormat = new SimpleDateFormat(fmt) ;
        String time = dateFormat.format(date);
        return time;
    }

    //转成instant 由本地传到服务端
    public static String toInstant(Date date){
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'") ;
        dateFormat.setTimeZone(TimeZone.getTimeZone("GMT+00:00"));
        String time = dateFormat.format(date);
        return time;
    }
    //instant转成date
    public static Date parseInInstant(String d){
        if(TextUtils.isEmpty(d)){
            return null;
        }
        d = d.replaceAll("T"," ");
        d = d.replaceAll("Z","");
        if(d.contains(".")){
            int i = d.indexOf(".");
            d = d.substring(0,i)+" +0000";
        }else{
            d+=" +0000";
        }
        return parse(d,"yyyy-MM-dd HH:mm:ss Z");
    }
    //install to str
    public static String parseInstantStr(String d){
        return format(parseInInstant(d),null);
    }
    /**
     * 把一个字符转换成对应的日期
     * @param d
     * @param fmt
     * @return
     */
    public static Date parse(String d, String fmt){
        if(fmt==null){
            fmt = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat(fmt) ;
        Date date = null;
        try {
            date = dateFormat.parse(d);
        }catch (Exception e){
        }
        return date;
    }

    public static int getCurrentMonthLastDay() {
        Calendar a = Calendar.getInstance();
        a.set(Calendar.DATE, 1);//把日期设置为当月第一天
        a.roll(Calendar.DATE, -1);//日期回滚一天，也就是最后一天
        int maxDate = a.get(Calendar.DATE);
        return maxDate;
    }

    public static String getCurrentYearAndMonth(){
        Calendar a = Calendar.getInstance();
        int year = a.get(Calendar.YEAR);
        int month = a.get(Calendar.MONTH) + 1;
        return year+"年"+month+"月";
    }

    public static int getFirstDayOfMonth(){
        Calendar a = Calendar.getInstance();
        a.set(Calendar.DAY_OF_MONTH,1);
        int i = a.get(Calendar.DAY_OF_WEEK);
        return i;
    }


    /**
     * 将sourceDate转换成指定时区的时间
     * @param sourceDate
     * @param sourceTimezone sourceDate所在的时区
     * @param targetTimezone 转化成目标时间所在的时区
     * @return
     */
    public static Date convertTimezone(Date sourceDate, TimeZone sourceTimezone, TimeZone targetTimezone){


        // targetDate - sourceDate=targetTimezone-sourceTimezone
        // --->
        // targetDate=sourceDate + (targetTimezone-sourceTimezone)


        Calendar calendar= Calendar.getInstance();
        // date.getTime() 为时间戳, 为格林尼治到系统现在的时间差,世界各个地方获取的时间戳是一样的,
        // 格式化输出时,因为设置了不同的时区,所以输出不一样
        long sourceTime=sourceDate.getTime();


        calendar.setTimeZone(sourceTimezone);
        calendar.setTimeInMillis(sourceTime);// 设置之后,calendar会计算各种filed对应的值,并保存

        //获取源时区的到UTC的时区差
        int sourceZoneOffset=calendar.get(Calendar.ZONE_OFFSET);


        calendar.setTimeZone(targetTimezone);
        calendar.setTimeInMillis(sourceTime);

        int targetZoneOffset=calendar.get(Calendar.ZONE_OFFSET);
        int targetDaylightOffset=calendar.get(Calendar.DST_OFFSET); // 夏令时


        long targetTime=sourceTime+ (targetZoneOffset+targetDaylightOffset) -sourceZoneOffset;

        return new Date(targetTime);

    }



    /**
     * 判断预约时间是否是同一天
     * @param date
     * @return
     */
    public static boolean checkYuYueIsToday(Date date){
        String today = format(new Date(),"yyyy-MM-dd");
        String yuyueDate = format(date,"yyyy-MM-dd");
        if(today.equals(yuyueDate)){
            return true;
        }
        return false;
    }


    //获取当前日期是星期几
    public static String getWeekOfDate(Date date) {
        String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        return weekDays[w];
    }

    //转换当前时间不包含时
    public static String parseTime(int oldTime) {
        SimpleDateFormat sdf = new SimpleDateFormat("mm:ss");// 时间格式
        String newTime = sdf.format(new Date(oldTime));
        return newTime;
    }
    //将长整型时间转为为分秒
    public static String toTime(long millionSeconds) {
        return toTime(millionSeconds,"mm:ss");
    }
    public static String toTime(long millionSeconds, String fmt) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(fmt);
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(millionSeconds);
        return simpleDateFormat.format(c.getTime());
    }
}
